package com.beeantec.jnrerc.modules.message.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.beeantec.jnrerc.core.annon.LogInject;
import com.beeantec.jnrerc.core.api.ApiRest;
import com.beeantec.jnrerc.core.api.controller.BaseController;
import com.beeantec.jnrerc.core.enums.MessageTypeEnum;
import com.beeantec.jnrerc.dto.MemberVo;
import com.beeantec.jnrerc.dto.response.MessageStateRespDTO;
import com.beeantec.jnrerc.entity.MemberMsg;
import com.beeantec.jnrerc.modules.member.service.MemberService;
import com.beeantec.jnrerc.modules.message.service.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Anthony
 */
@Api(tags={"系统管理员"})
@RestController
@RequestMapping("/api/message")
public class MessageController extends BaseController {

    @Autowired
    private MemberService memberService;
    @Autowired
    private MessageService messageService;

    /**
     * 获取消息
     * @return
     */
    @LogInject(title = "获取消息列表")
    @CrossOrigin
    @ApiOperation(value = "获取消息列表")
    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    public ApiRest<List<MemberMsg>> list(@RequestHeader("token") String token, @RequestParam("type") Integer type) {
        MemberVo memberVo = memberService.token(token);
        List<MemberMsg> messageList ;
        if (type.equals(MessageTypeEnum.SYSTEM.getCode()) || type.equals(MessageTypeEnum.MISSION.getCode()) ||type.equals(MessageTypeEnum.LEARN.getCode())){
            QueryWrapper<MemberMsg> messageQueryWrapper = new QueryWrapper<>();
            messageQueryWrapper.lambda().eq(MemberMsg::getUserId,memberVo.getMemberId())
                    .eq(MemberMsg::getType,type)
            .orderByDesc(MemberMsg::getCreateTime);
            messageList  = messageService.getBaseMapper().selectList(messageQueryWrapper);
            return super.success(messageList);
        }
        messageList = messageService.list(new QueryWrapper<MemberMsg>().lambda().eq(MemberMsg::getUserId,memberVo.getMemberId()).orderByDesc(MemberMsg::getCreateTime));
        return super.success(messageList);
    }



    /**
     * 是否有未读消息
     * @return
     */
    @LogInject(title = "是否有未读消息")
    @CrossOrigin
    @ApiOperation(value = "是否有未读消息")
    @RequestMapping(value = "/readstate", method = {RequestMethod.GET})
    public ApiRest<MessageStateRespDTO> readstate(@RequestHeader("token") String token) {
        MessageStateRespDTO messageStateRespDTO = new MessageStateRespDTO();
        MemberVo memberVo = memberService.token(token);
        List<MemberMsg> systemMsgList = new ArrayList<>();
        List<MemberMsg> missionMsgList = new ArrayList<>();
        List<MemberMsg> learnMsgList = new ArrayList<>();
            QueryWrapper<MemberMsg> messageQueryWrapper1 = new QueryWrapper<>();
            messageQueryWrapper1.lambda().eq(MemberMsg::getUserId,memberVo.getMemberId())
                    .eq(MemberMsg::getType,MessageTypeEnum.SYSTEM.getCode()).orderByDesc(MemberMsg::getCreateTime)
                    .eq(MemberMsg::getReadState,1);
        systemMsgList  = messageService.list(messageQueryWrapper1);
            QueryWrapper<MemberMsg> messageQueryWrapper2 = new QueryWrapper<>();
            messageQueryWrapper2.lambda().eq(MemberMsg::getUserId,memberVo.getMemberId())
                    .eq(MemberMsg::getType,MessageTypeEnum.MISSION.getCode()).orderByDesc(MemberMsg::getCreateTime)
                    .eq(MemberMsg::getReadState,1);
        missionMsgList  = messageService.list(messageQueryWrapper2);
            QueryWrapper<MemberMsg> messageQueryWrapper3 = new QueryWrapper<>();
            messageQueryWrapper3.lambda().eq(MemberMsg::getUserId,memberVo.getMemberId())
                    .eq(MemberMsg::getType,MessageTypeEnum.LEARN.getCode()).orderByDesc(MemberMsg::getCreateTime)
                    .eq(MemberMsg::getReadState,1);
        learnMsgList  = messageService.list(messageQueryWrapper3);
        messageStateRespDTO.setSystemUnread(systemMsgList.size());
        messageStateRespDTO.setMissionUnread(missionMsgList.size());
        messageStateRespDTO.setLearnUnread(learnMsgList.size());
            return super.success(messageStateRespDTO);
    }
}
